<%--
  Created by IntelliJ IDEA.
  User: carol
  Date: 2018/6/16
  Time: 20:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*,java.sql.*,javax.servlet.*,functest.*"%>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="layui/css/layui.css" media="all">
    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="js/jquery-3-2-1.js"></script>
    <meta charset="UTF-8">
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String action = request.getParameter("action");
    String hb_name = request.getParameter("hbname");
    String plane_id = request.getParameter("plane");
    String topseat = request.getParameter("topseat");
    String ecoseat = request.getParameter("ecoseat");
    FindAPrint func =new FindAPrint();

    String driver="com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
    String user="hbpwuser";
    String pass="LFFLXY";
    Class.forName(driver);                   //加载jdbc驱动
    Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);
    if (action == null){//未传入action
        Statement statement = (Statement) connection.createStatement();  //连接实例
        String checksql = "select count(*) as total from spxx where HB_HBH='" + hb_name + "';";
        ResultSet rst1 = statement.executeQuery(checksql);
        if (rst1.next()){
            int hdtotal = rst1.getInt("total");
            if (hdtotal > 0) {
%>
<blockquote class="layui-elem-quote layui-quote-nm">无法编辑，请删除有关航班所有购票信息</blockquote>
<%
            }
            else {//输出添加表单
%>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>编辑航班</legend>
</fieldset>
<form class="layui-form" method="post" style="margin: 20px" action="edithb.jsp?action=update">
    <div class="layui-form-item">
        <label class="layui-form-label">航班名称</label>
        <div class="layui-input-block">
            <input type="text" name="hbname" lay-verify="name" readonly autocomplete="on" class="layui-input" value="<%=hb_name%>">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">飞机机型</label>
        <div class="layui-input-block">
            <input type="text" required="required" name="planename" lay-verify="required" autocomplete="on" class="layui-input" value="<%=plane_id%>">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">航程安排</label>
        <div class="layui-input-block">
            <input type="checkbox" name="airplan" value="0" title="周日">
            <input type="checkbox" name="airplan" value="1" title="周一">
            <input type="checkbox" name="airplan" value="2" title="周二">
            <input type="checkbox" name="airplan" value="3" title="周三">
            <input type="checkbox" name="airplan" value="4" title="周四">
            <input type="checkbox" name="airplan" value="5" title="周五">
            <input type="checkbox" name="airplan" value="6" title="周六">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头等舱数</label>
        <div class="layui-input-block">
            <input type="number" min="1" required="required" name="topnumber" min="<%=topseat%>"
                   lay-verify="number" autocomplete="on" class="layui-input" value="<%=topseat%>">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">经济舱数</label>
        <div class="layui-input-block">
            <input type="number" min="1" required="required" name="econumber" min="<%=ecoseat%>" lay-verify="number"
                   placeholder="请输入" autocomplete="on" class="layui-input" value="<%=ecoseat%>">
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <button class="layui-btn" lay-submit="" id="submit">确认提交</button>
    </div>
</form>
<script type="text/javascript">
    $(function(){
        $('#submit').click(function(){
            if($("input[name='airplan']:checked").length == 0){
                alert("最少选择一个时间!");
                return false;
            }
            else $('form').submit();
        });
    })
</script>
<%
        }
    }
}
else{


    String[] checkplan = request.getParameterValues("airplan");
    StringBuilder temp = new StringBuilder("0000000");
    for (int i = 0; i < checkplan.length; i ++){
        int checktemp = Integer.parseInt(checkplan[i]);
        temp.replace(checktemp,checktemp+1,"1");
    }
    String airplan = temp.toString();
    String plane_name = request.getParameter("planename");
    String top_number = request.getParameter("topnumber");
    String eco_number = request.getParameter("econumber");
    Statement state = (Statement) connection.createStatement();  //连接实例
    String insertsql = "update hb set HB_FJJX = '"+ plane_name + "', HB_HBHCAP='" + airplan
            + "',HB_TDCS=" + top_number + ",HB_JJCS=" + eco_number + " where HB_HBH ='" + hb_name + "';";
    try {
        state.executeUpdate(insertsql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">编辑成功</blockquote>
<%
                }catch (SQLException e){
                    out.println(insertsql);
                    out.println(e);
                }


    }
%>
</body>
</html>
<script type="text/javascript">
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.verify({
            number: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[0-9\\s·]+$").test(value)){
                    return '请输个正常的数字';
                }
            }
        });
        form.render();
    })
</script>
